package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.log.MethodLog;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IAdvanceSaleService;
import com.wondertek.poms.core.service.IContentService;
import com.wondertek.poms.core.service.IImageService;
import com.wondertek.poms.dao.po.AdvanceSale;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Objects;

/**
 * @author gzx
 * @description 图片接口
 */
@Api(value = "ImageController", description = "图片 接口")
@RestController
@RequestMapping("image")
@Slf4j
public class ImageController {

	@Resource
	private HttpServletResponse response;

	@Autowired
	private IImageService imageService;

	/**
	 * 查询内容或节目图片列表
	 * @param contId 内容id
	 * @return 图片列表
	 */
	@ApiOperation(value = "图片列表", notes = "图片列表")
	@MethodLog(module = "图片列表")
	@GetMapping(value = "/get")
	public ResultBean getImages(String contId) throws IOException {
		try {
			return ResultBean.ok(imageService.getAllImages(contId));
		}catch (Exception e){
			return ResultBean.error(e.toString());
		}
	}

	/**
	 *
	 * @param imgPath 图片路径
	 * @return 图片文件
	 */
	@ApiOperation(value = "展示图片", notes = "展示图片")
	@MethodLog(module = "展示图片")
	@RequestMapping(value = "/show", produces = MediaType.IMAGE_JPEG_VALUE)
	public BufferedImage list(String imgPath) throws IOException {
		if (StringUtils.isNotBlank(imgPath)) {
			File f = new File(imgPath);
			if (!f.exists()) {
				return null;
			}
			int start = imgPath.lastIndexOf(".");
			if (start != -1) {
				String suffix = imgPath.substring(start+1);
				OutputStream os = null;
				try {
					//读取图片
					BufferedImage image = ImageIO.read(new FileInputStream(f));
					response.setContentType("image/" + suffix);
					os = response.getOutputStream();
					if (image != null) {
						ImageIO.write(image, suffix, os);
					}
				} catch (IOException e) {
					log.error("获取图片异常{}", e.getMessage());
				} finally {
					if (os != null) {
						os.flush();
						os.close();
					}
				}
			}
		}
		return null;
	}


	/**
	 *
	 * @param imgPath 图片路径
	 * @return 图片文件
	 */
	@ApiOperation(value = "展示图片base64流", notes = "展示图片base64流")
	@MethodLog(module = "展示图片base64流")
	@RequestMapping(value = "/show-base64", produces = MediaType.IMAGE_JPEG_VALUE)
	public ResultBean showBase64(String imgPath) throws IOException {
		if (StringUtils.isNotBlank(imgPath)) {
			File file = new File(imgPath);
			if (!file.exists()) {
				return ResultBean.error("没有找到图片");
			}
			byte[] data = null;
			// 读取图片字节数组
			try {
				InputStream input = new FileInputStream(imgPath);
				data = new byte[input.available()];
				input.read(data);
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			// 对字节数组Base64编码
			BASE64Encoder encoder = new BASE64Encoder();
			// 返回Base64编码过的字节数组字符串
			return ResultBean.ok(encoder.encode(data));
		}
		return ResultBean.error("没有找到图片，请确认图片路径。");
	}

}
